<?php
namespace app\admin\controller;
use app\common\model;
use app\common\model\User;
use app\common\model\Student;
use app\service\OrderService;
use app\service\MsgService;
use app\common\model\Daily;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

header('Access-Control-Allow-Origin:*');

class Index extends Base
{
    public $openPlatform;
    public function __construct () {
      parent::__construct();
    }
    public function index()
    {
        MsgService::S([
            'mall_id' => $this->mall['id']
        ])->resetMallTmpl($this->mall['id']);
    }

   
    //返回信息
    public function backCensus(){
        //查询订单统计信息
        $today_num_where = [];
        $today_num_where['create_time']=array(array('EGT',strtotime(date('Y-m-d'))),array('ELT',time()),"AND");
        $data[]=array(
            'num'=>Student::getCount($today_num_where),
            'cname'=>'今日新增队员（人）'
        );
        $today_num_where['create_time']=array(array('EGT',strtotime(date('Y-m-d'),'-1 day')),array('LT',strtotime(date('Y-m-d'))),"AND");
        $data[]=array(
            'num'=>Student::getCount($today_num_where),
            'cname'=>'昨日新增队员（人）'
        );
        $today_num_where['create_time']=array(array('EGT',mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"))),array('LT',time()),"AND");
        $data[]=array(
            'num'=>Student::getCount($today_num_where),
            'cname'=>'本周新增队员（人）'
        );
        $data[]=array(
            'num'=>Student::getCount([]),
            'cname'=>'所有新增队员（人）'
        );
        $today_num_where['create_time']=array(array('EGT',strtotime(date('Y-m-d'))),array('ELT',time()),"AND");
        $data[]=array(
            'num'=>User::getCount($today_num_where),
            'cname'=>'今日新增用户（人）'
        );
        $today_num_where['create_time']=array(array('EGT',strtotime(date('Y-m-d'),'-1 day')),array('LT',strtotime(date('Y-m-d'))),"AND");
        $data[]=array(
            'num'=>User::getCount($today_num_where),
            'cname'=>'昨日新增用户（人）'
        );
        $today_num_where['create_time']=array(array('EGT',mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"))),array('LT',time()),"AND");
        $data[]=array(
            'num'=>User::getCount($today_num_where),
            'cname'=>'本周新增用户（人）'
        );
        $data[]=array(
            'num'=>User::getCount([]),
            'cname'=>'所有新增用户（人）'
        );

        echo json_encode(array('code'=>0,'data'=>$data));exit();
    }
    public function backCensus1(){
        //查询订单统计信息
        $today_order_num_where = [];
        if($this->mall) {
            $today_order_num_where['mall_id'] = $this->mall['id'];
        }
        $today_order_num_where['create_time']=array(array('EGT',strtotime(date('Y-m-d'))),array('ELT',time()),"AND");
        $today_order_num_where['checked']=array('in',array(1,2,5));
        $data[]=array(
            'num'=>OrderService::S()->countOrder($today_order_num_where),
            'cname'=>'今日成交（笔）'
        );
        $data[]=array(
            'num'=>OrderService::S()->sumOrder($today_order_num_where,'all_money'),
            'cname'=>'今日消费金额（元）'
        );
        $today_order_num_where['create_time']=array(array('EGT',strtotime(date('Y-m-d'),'-1 day')),array('LT',strtotime(date('Y-m-d'))),"AND");
        $data[]=array(
            'num'=>OrderService::S()->countOrder($today_order_num_where),
            'cname'=>'昨日成交（笔）'
        );
        $data[]=array(
            'num'=>OrderService::S()->sumOrder($today_order_num_where,'all_money'),
            'cname'=>'昨日消费金额(元)'
        );
        $today_order_num_where['create_time']=array(array('EGT',mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"))),array('LT',time()),"AND");
        $data[]=array(
            'num'=>OrderService::S()->countOrder($today_order_num_where),
            'cname'=>'本周成交（笔）'
        );
        $data[]=array(
            'num'=>OrderService::S()->sumOrder($today_order_num_where,'all_money'),
            'cname'=>'本周消费金额（笔）'
        );
        $today_order_num_where['create_time']=array(array('EGT',strtotime(date('Y-m-1'))),array('LT',time()),"AND");
        $data[]=array(
            'num'=>OrderService::S()->countOrder($today_order_num_where),
            'cname'=>'本月成交（笔）'
        );
        $data[]=array(
            'num'=>OrderService::S()->sumOrder($today_order_num_where,'all_money'),
            'cname'=>'本月成交金额（笔）'
        );

        $today_order_num_where['create_time']=array(array('EGT',strtotime(date('Y-01-01'))),array('LT',time(),"AND"));
        $data[]=array(
            'num'=>OrderService::S()->countOrder($today_order_num_where),
            'cname'=>'今年成交（笔）'
        );
        $data[]=array(
            'num'=>OrderService::S()->sumOrder($today_order_num_where,'all_money'),
            'cname'=>'今年成交金额（笔）'
        );

        //$today_order_num_where['create_time']=array(array('EGT',strtotime(date('Y-01-01'))),array('LT',time(),"AND"));
        $data[]=array(
            'num'=>OrderService::S()->countOrder($today_order_num_where),
            'cname'=>'所有成交（笔）'
        );
        $data[]=array(
            'num'=>OrderService::S()->sumOrder($today_order_num_where,'all_money'),
            'cname'=>'所有成交金额（笔）'
        );

        echo json_encode(array('code'=>0,'data'=>$data));exit();
    }

    /* 每日统计 */
    /* 每日统计 */
    public function daily () {
        $input = input();
        $start = $input['start']?$input['start']:date('Y-m-d','-30 day');
        $end = $input['end']?$input['end']:date('Y-m-d');
        $list = Daily::getAll([
            'create_date' => [
                'between',[$start,$end]
            ]
        ],'create_date desc');
        if($input['excel']) {
            $file = $this->exportExcel($list);
            return returnJson(0,'',$file);
        }
        $result['header'] = [
            ['日期','create_date'],
            ['当日新注册用户','register'],
            ['总用户数','user_all'],
            ['当日新增群主','grade2_add'],
            ['群主总数','grade2_all'],
            ['当日新增分公司','grade3_add'],
            ['分公司总数','grade3_all'],
            ['当日页面访问量','pv'],
            ['当日用户访问量','uv'],
            ['当日支付金额','order_money'],
            ['当日支付订单数','order_number'],
            ['当日支付商品数','order_goods'],
            ['当日支付买家数','order_user'],
            ['当日支付转化率','order_trans_rate'],
            ['客单价','order_per_money'],
            ['当日积分商城订单数','order_score_number']
        ];
        $result['data'] = $list;
        return returnJson(0,'',$result);
    }

    protected function exportExcel ($list) {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $arrData = [
          ['日期','当日新注册用户','总用户数','当日新增会员','会员总数','当日新增加盟商','加盟商总数','当日新增总代','总代总数','当日页面访问量','当日用户访问量','当日支付金额','当日支付订单数',
          '当日支付商品数','当日支付买家数','当日支付转化率','客单价','当日积分商城订单数']
        ];
        foreach($list as $k=>$v) {
          $data = [
            $v['create_date'],$v['register'],$v['user_all'],$v['grade2_add'],$v['grade2_all'],$v['grade3_add'],$v['grade3_all'],$v['grade4_add'],$v['grade4_all'],
            $v['pv'],$v['uv'],$v['order_money'],$v['order_number'],$v['order_goods'],$v['order_user'],$v['order_trans_rate'],$v['order_per_money'],$v['order_score_number']
          ];
          foreach($data as $kk=>$vv) {
              $data[$kk] = (string)$vv;
          }
          $arrData[] = $data;
        }
        $sheet->fromArray($arrData,NULL);
        $writer = new Xlsx($spreadsheet);
        $name = '统计信息-'.date('Y-m-d').'-'.time().".xlsx";
        $path = ROOT_PATH."public".DS."cache".DS.$name;
        $writer->save($path);
        return request()->domain()."/cache/".$name;
    }

}
